﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Helper;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using Company.Sqlite.Repositories;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using 无人值守地磅称重系统.Dialogs;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Views;

namespace 无人值守地磅称重系统.ViewModels
{
    public class StuffViewModel : ObservableObject
    {
        public Stuff NewStuff { get; set; } = new Stuff();

        private List<Stuff> stuffs;

        public List<Stuff> Stuffs { get => stuffs; set => SetProperty(ref stuffs, value);  }

        public ICommand InsertCommand { get; }

        public ICommand UpdateCommand { get; }

        public ICommand DeleteCommand { get; }

        public ICommand SearchCommand { get; }

        public ICommand ClearKeywordCommand { get; }

        public IStuffRepository StuffRepository { get; }

        public ISession Session { get; set; }

        public StuffViewModel(ISession session, IStuffRepository stuffRepository)
        {
            Session = session;
            StuffRepository = stuffRepository;
            Stuffs = StuffRepository.GetAll();

            InsertCommand = new RelayCommand(Insert);
            UpdateCommand = new RelayCommand<Stuff>(Update);
            DeleteCommand = new RelayCommand<Stuff>(Delete);
            SearchCommand = new RelayCommand<string>(Search);
            ClearKeywordCommand = new RelayCommand<TextBox>(ClearKeyword);
        }

        private void ClearKeyword(TextBox textBox)
        {
            textBox.Text = string.Empty;
            StuffRepository.GetAll();
        }

        private void Search(string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                Stuffs = StuffRepository.GetAll();
            }
            else
            {
                Stuffs = StuffRepository.GetAll(keyword);
            }
        }

        private async void Delete(Stuff stuff)
        {
            var view = new Dialog("是否执行删除操作？", System.Windows.MessageBoxButton.YesNo);
            bool result = (bool)await DialogHost.Show(view, "RootDialog");

            if (result)
            {
                int count = StuffRepository.Delete(stuff);
                if (count > 0)
                {
                    Session.Show("删除成功！");
                    Stuffs = StuffRepository.GetAll();
                }
            }
        }

        private async void Update(Stuff entity)
        {
            var newEntity = entity.DeepClone();
            var view = new UpdateStuffView
            {
                DataContext = new UpdateViewModel<Stuff>(newEntity)
            };
            bool result = (bool)await DialogHost.Show(view, "RootDialog");
            if (result)
            {
                entity.Name= newEntity.Name;
                entity.Spacification = newEntity.Spacification;
                entity.Price = newEntity.Price;
                int count = StuffRepository.Update(entity);
                if(count > 0)
                {
                    Session.Show("修改成功");
                }
            }
        }

        private void Insert()
        {
            if (string.IsNullOrEmpty(NewStuff.Name))
            {
                Session.Show("请输入物资名");
                return;
            }

            if(StuffRepository.Select(NewStuff.Name) != null)
            {
                Session.Show("该物资已存在");
                return;
            }

            Stuff stuff = new Stuff();
            stuff.Name= NewStuff.Name;
            stuff.Spacification= NewStuff.Spacification;
            stuff.Price= NewStuff.Price;
            stuff.Rank = NewStuff.Rank;

            int count  = StuffRepository.Insert(stuff);
            if(count > 0)
            {
                Session.Show("添加成功");
                Stuffs = StuffRepository.GetAll();
                NewStuff.Name = string.Empty;
                NewStuff.Spacification= string.Empty;
                NewStuff.Price = 0d;
            }
        }
    }
}
